/*
 * Copyright (c) 2010 Les Newell

 * HMC.h - Interface a Honeywell HMC5843 magnetometer to an AVR via i2c
 * Version 0.1 - http://eclecti.cc
 * Copyright (c) 2009 Nirav Patel
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 *
 * Code based in part on information from the following:
 * http://www.sparkfun.com/datasheets/Sensors/Magneto/HMC5843-v11.c
 * http://www.atmel.com/dyn/resources/prod_documents/doc2545.pdf
 */


#include "Compass.h"

DEFINE_MODULE(HMC5843,Compass3Axis)

public:
    HMC5843()
    {
    }

    virtual void Init()
    {
        SetInterval(20);
        SetPriority(6);
        init(HZ500);
        Compass3Axis::Init();
    }

    virtual void ParamChanged(Socket * param)
    {
        Compass3Axis::ParamChanged(param);
    }

    virtual void Loop(const unsigned long& interval)
    {
        int x,y,z;
        getValues(&x,&y,&z);
        m_axes[HMC5843_X] = (HMC5843_X_DIRECTION * x) + m_offsets[HMC5843_X];
        m_axes[HMC5843_Y] = (HMC5843_Y_DIRECTION * y) + m_offsets[HMC5843_Y];
        m_axes[HMC5843_Z] = (HMC5843_Z_DIRECTION * z) + m_offsets[HMC5843_Z];
        CalculateHeading();
    }

private:

    enum RATE{HZ005,HZ010,HZ020,HZ050,HZ100,HZ200,HZ500};
    // note that you need to wait at least 5ms after power on to initialize
    void init(RATE rate)
    {
        //  PORTC = 0b00110000; // Use the internal pull up resistors

        // Choose 100KHz for the bus.  Formula from 21.5.2 in ATmega168 datasheet.
        TWSR &= ~((1<<TWPS1)&(1<<TWPS0));
        TWBR = (unsigned char)(F_CPU/200000l-8);

        // Initialize the HMC5843
        sendStart();
        sendByte(0x3C); //write
        sendByte(0x00); //address 0 (CRA)
        sendByte(rate << 2); //CRA = sample rate, normal measurement
        sendByte(0x00); //CRB= 0.7Ga range
        sendByte(0x00); //Mode= continuous
        sendStop();
        // note that you need to wait 100ms after this before first calling recieve
    }

    // This can be called at intervals to get new data
    void getValues(int *x, int *y, int *z)
    {
        // start the reading
        sendStart();
        sendByte(0x3D);
        // read out the 3 values, 2 bytes each.  lsb first, then msb.
        *x = (receiveByte() << 8) | receiveByte();
        *y = (receiveByte() << 8) | receiveByte();
        *z = (receiveByte() << 8) | receiveByte();
        // wrap back around for the next set of reads and close
        sendByte(0x3D);
        sendStop();
    }

    // start i2c as the master
    void sendStart(void)
    {
        TWCR = (1<<TWINT)|(1<<TWSTA)|(1<<TWEN);
    }

    // close i2c
    void sendStop(void)
    {
        waitForReady();
        TWCR = (1<<TWINT)|(1<<TWEN)|(1<<TWSTO);
    }

    // send a byte when the channel is ready
    void sendByte(unsigned char data)
    {
        waitForReady();
        TWDR = data;
        TWCR = (1<<TWINT)|(1<<TWEN);
    }

    // ask for a byte, wait for it arrive, and return it
    unsigned char receiveByte(void)
    {
        waitForReady();
        TWCR = ((TWCR&0x0F)|(1<<TWINT)|(1<<TWEA));
        waitForReady();
        return(TWDR);
    }

    // get status register. the bits 0 and 1 are zeroed in init. see datasheet
    unsigned char readStatus()
    {
        waitForReady();
        return(TWSR);
    }

    // wait for TWINT to be set before touching the other registers.
    void waitForReady(void)
    {
        // timeout after some time to avoid locking up if something goes wrong
        int timeout = 200;
        while ((!(TWCR & (1<<TWINT))) && (timeout--)){};
    }

};

HMC5843 g_HMC5843;

